package com.haochang.chunkvod.controller.fragment.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.service.ServiceData;
import com.haochang.chunkvod.model.service.ServiceInfo;
import com.haochang.chunkvod.view.vod.activity.CustomDialogActivity;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * @author qintao
 *         created at 2016/6/13 10:38
 *         服务新增
 */

public class ServiceAddFragment extends BaseFragment
{
	private TitleView titleView;
	private BaseListView listView;
	private BaseTextView tv_search;
	private QuickAdapter dataAdapter;
	private List<ServiceInfo> infoList = new ArrayList<ServiceInfo>();
	private ServiceData serviceData;
	private String roomId = "";
	private String roomNum = "";

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_add_service, container, false);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		initView();
		initData();
	}

	private void initData()
	{
		serviceData = new ServiceData(getActivity());
		serviceData.requestServiceShortcut("", new ServiceData.IRequestServiceListener()
		{
			@Override
			public void onRequestSuccess(List<ServiceInfo> list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					infoList.clear();
					infoList.addAll(list);
					dataAdapter.replaceAll(infoList);
				}
			}
		});
	}

	private void initView()
	{
		roomNum = getArguments().getString(IntentKey.ROOM_NUM);
		roomId = getArguments().getString(IntentKey.ROOM_ID);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(roomNum).setBackText("主页").setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				getActivity().finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(getActivity(), list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							((BaseActivity) getActivity()).loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		listView = (BaseListView) findViewById(R.id.listView);
		tv_search = (BaseTextView) findViewById(R.id.tv_search);
		tv_search.setVisibility(View.GONE);
		tv_search.setOnClickListener(mOnBaseClickListener);
		dataAdapter = new QuickAdapter<ServiceInfo>(getActivity(), R.layout.item_service_add_fragment_layout, infoList)
		{

			@Override
			protected void convert(BaseAdapterHelper helper, final ServiceInfo item)
			{
				helper.setText(R.id.tv_foodName, item.getService_content());
				helper.getView().setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Intent intent = new Intent(getActivity(), CustomDialogActivity.class);
						intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, item.getService_content());
						intent.putExtra(IntentKey.SERVICE_GOODS_ID, item.getService_id());
						intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
						intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, "1");
						intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.CUSTOM_VIEW_TITLE.toString());
						intent.putExtra(IntentKey.SURPLUS_NUMBER, -1);
						intent.putExtra(IntentKey.ROOM_ID, roomId);
						intent.putExtra(IntentKey.SEARCH_TYPE, VodConstant.NO_SEARCH);
						startActivity(intent);
					}
				});
			}
		};
		listView.setAdapter(dataAdapter);
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_search:
				Intent intent = new Intent(getActivity(), CustomDialogActivity.class);
				intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "内容");
				intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
				intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, "1");
				intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.CUSTOM_VIEW_TITLE.toString());
				intent.putExtra(IntentKey.SURPLUS_NUMBER, -1);
				intent.putExtra(IntentKey.ROOM_ID, roomId);
				intent.putExtra(IntentKey.SEARCH_TYPE, VodConstant.SEARCH);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

}
